package edu.ucsd.db.bassarisk.columndef;

import edu.ucsd.db.bassarisk.exception.VisitorException;
import edu.ucsd.db.bassarisk.query.path.DSDElementVisitor;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDAggregate;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDContext;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDDescent;
import edu.ucsd.db.bassarisk.query.path.DSDElements.DSDFilter;

public class DCDAggregationConextFinder implements
    DSDElementVisitor<DSDDescent, DSDDescent> {
  @Override
  public DSDDescent visitAggregate(final DSDAggregate elem, DSDDescent parent)
      throws VisitorException {
    return parent;
  }

  @Override
  public DSDDescent visitContext(DSDContext elem, DSDDescent parent)
      throws VisitorException {
    if (elem.hasNext()) {
      return elem.getNext().accept(this, parent);
    }
    return parent;
  }

  @Override
  public DSDDescent visitDescent(DSDDescent elem, DSDDescent parent)
      throws VisitorException {
    if (elem.isFilter()) {
      return parent;
    }
    if (elem.hasNext()) {
      return elem.getNext().accept(this, elem);
    }
    return elem;
  }

  @Override
  public DSDDescent visitFilter(DSDFilter elem, DSDDescent parent) throws VisitorException {
    if (elem.hasNext()) {
      return elem.getNext().accept(this, parent);
    }
    return parent;
  }
}
